Imports System
Imports System.Data
Imports System.Configuration
Imports System.IO
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class ExportGridView

    Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView)
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
        HttpContext.Current.Response.ContentType = "application/ms-excel"
        HttpContext.Current.Response.Charset = "utf-8"
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250")

        Dim sw As StringWriter = New StringWriter
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        '  Create a form to contain the grid
        Dim table As Table = New Table
        table.GridLines = gv.GridLines
        SetStyles(table, gv)
        '  add the header row to the table
        If (Not (gv.HeaderRow) Is Nothing) Then
            ExportGridView.Massage(gv.HeaderRow)
            Dim i As Integer = 0
            i = table.Rows.Add(gv.HeaderRow)
            SetStyles(table.Rows(i), gv.HeaderStyle)
        End If
        '  add each of the data rows to the table
        For Each row As GridViewRow In gv.Rows
            ExportGridView.Massage(row)
            table.Rows.Add(row)
        Next
        '  add the footer row to the table
        If (Not (gv.FooterRow) Is Nothing) Then
            ExportGridView.Massage(gv.FooterRow)
            table.Rows.Add(gv.FooterRow)
        End If
        '  render the table into the htmlwriter
        table.RenderControl(htw)
        '  render the htmlwriter into the response
        HttpContext.Current.Response.Write(sw.ToString)
        HttpContext.Current.Response.End()
    End Sub
    Private Shared Sub Massage(ByVal localControl As Control)
        Dim iCount As Integer = 0
        Do While (iCount < localControl.Controls.Count)
            Dim currentControl As Control = localControl.Controls(iCount)
            If (TypeOf currentControl Is LinkButton) Then
                localControl.Controls.Remove(currentControl)
                localControl.Controls.AddAt(iCount, New LiteralControl(CType(currentControl, LinkButton).Text))
            ElseIf (TypeOf currentControl Is DropDownList) Then
                localControl.Controls.Remove(currentControl)
                localControl.Controls.AddAt(iCount, New LiteralControl(CType(currentControl, DropDownList).SelectedItem.Text))
            ElseIf (TypeOf currentControl Is HyperLink) Then
                localControl.Controls.Remove(currentControl)
                localControl.Controls.AddAt(iCount, New LiteralControl(CType(currentControl, HyperLink).Text))
            ElseIf (TypeOf currentControl Is CheckBox) Then
                localControl.Controls.Remove(currentControl)
                localControl.Controls.AddAt(iCount, New LiteralControl(CType(currentControl, CheckBox).Checked))
            ElseIf (TypeOf currentControl Is ImageButton) Then
                localControl.Controls.Remove(currentControl)
                localControl.Controls.AddAt(iCount, New LiteralControl(CType(currentControl, ImageButton).AlternateText))
            End If
            If currentControl.HasControls Then
                ExportGridView.Massage(currentControl)
            End If
            iCount += 1
        Loop
    End Sub
    Private Shared Sub SetStyles(ByRef HTMLTable As Object, ByVal FromObject As Object)
        Try
            HTMLTable.BackColor = FromObject.BackColor
            HTMLTable.BorderColor = FromObject.BorderColor
            HTMLTable.ForeColor = FromObject.ForeColor
            HTMLTable.BackImageUrl = FromObject.BackImageUrl
            HTMLTable.Font.Name = FromObject.Font.Name
            HTMLTable.Font.Italic = FromObject.Font.Italic
            HTMLTable.Font.Bold = FromObject.Font.Bold
            HTMLTable.Font.Size = FromObject.Font.Size
            HTMLTable.Font.Underline = FromObject.Font.Underline
            HTMLTable.Font.Strikeout = FromObject.Font.Strikeout

        Catch ex As Exception

        End Try
    End Sub
End Class

